package com.toommi.refuse.controller;

import com.toommi.refuse.common.response.CommonResponse;
import com.toommi.refuse.entity.PageImages;
import com.toommi.refuse.service.PageImagesService;
import com.toommi.refuse.service.RefuseTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: IndexImagesController
 * @Description: 页面图片维护
 * @Auther: HuangBo
 * @Date: 2019/9/12 0012 11:37
 */
@RestController
@Slf4j
@RequestMapping("/api/indeximg")
public class PageImagesController {
    @Autowired
    private PageImagesService pageImagesService;
    @Autowired
    private RefuseTypeService refuseTypeService;

    /**
     * 功能描述: 上传图片的接口
     *
     * @param: [file, request]
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/12 0012 下午 16:58
     */
    @PostMapping(value = "/upload")
    public CommonResponse uploadImage(MultipartFile file, HttpServletRequest request) {
        CommonResponse commonResponse = CommonResponse.success();
        try {
            pageImagesService.upload(file, request);
            return commonResponse;
        } catch (IOException e) {
            log.error("imge upload error : {}", e);
            commonResponse = CommonResponse.serverError();
            return commonResponse;
        }
    }


    /**
     * 功能描述: 获取图片用途（可回收分类）列表
     *
     * @param: []
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/12 0012 下午 16:59
     */
    @GetMapping("/imguselist")
    public CommonResponse getImgeUse() {
        CommonResponse response = CommonResponse.success();
        try {
            response.setData(refuseTypeService.getTypeNameList());
            return response;
        } catch (Exception e) {
            log.error("load type name of refuse error : {}", e);
            response = CommonResponse.serverError();
            return response;
        }
    }

    /**
     * 功能描述: 新增图片接口
     *
     * @param: [image]
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/12 0012 下午 17:09
     */
    @PostMapping(value = "/addimg", produces = "application/json;charset=UTF-8")
    public CommonResponse addImage(@RequestBody PageImages image) {
        CommonResponse response = CommonResponse.success();
        try {
            int counts = pageImagesService.addImage(image);
            if (counts < 1) {
                response.setMessage("添加失败");
                response.setCode(201);
                return response;
            } else {
                return response;
            }
        } catch (Exception e) {
            log.error("add image error : {}", e);
            response = CommonResponse.serverError();
            return response;
        }
    }

    /**
     * 功能描述: 分页获取所有图片
     *
     * @param: [page, limit]
     * @return: java.lang.Object
     * @auther: HuangBo
     * @date: 2019/9/12 0012 下午 16:59
     */
    @GetMapping(value = "/list", produces = "application/json;charset=UTF-8")
    public Object getAllImages(@RequestParam int page, @RequestParam int limit) {
        try {
            Object allImages = pageImagesService.findAllImages(page, limit);
            return allImages;
        } catch (Exception e) {
            log.error("get images error : {}", e);
            return null;
        }
    }


    /**
     * 功能描述: 获取图片列表
     *
     * @param: []
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/17 0017 上午 11:58
     */
    @GetMapping("/imagelist")
    public CommonResponse getAllImagesListByImagepage(Integer imgpage) {
        CommonResponse response = CommonResponse.success();
        try {
            List<PageImages> allImagesList = pageImagesService.findAllImagesListByImagepage(imgpage);
            response.setData(allImagesList);
            return response;
        } catch (Exception e) {
            log.error("get images list error:{}", e);
            response = CommonResponse.serverError();
            return response;
        }
    }


    /**
     * 功能描述: 根据id获取图片信息
     *
     * @param: [id]
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/13 0013 上午 0:42
     */
    @GetMapping(value = "/info")
    public CommonResponse getImageById(Integer id) {
        CommonResponse response = CommonResponse.success();
        try {
            PageImages image = pageImagesService.findOneById(id);
            response.setData(image);
            return response;
        } catch (Exception e) {
            log.error("get image info error:{}", e);
            response = CommonResponse.serverError();
            return response;
        }
    }

    /**
     * 功能描述: 根据id修改图片信息
     *
     * @param: [image]
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/13 0013 上午 2:04
     */
    @PostMapping(value = "/updateimg", produces = "application/json;charset=UTF-8")
    public CommonResponse updateImageById(@RequestBody PageImages image) {
        CommonResponse response = CommonResponse.success();
        try {
            pageImagesService.updateImageById(image);
            return response;
        } catch (Exception e) {
            log.error("update image error:{}", e);
            response = CommonResponse.serverError();
            return response;
        }
    }

    /**
     * 功能描述: 根据id删除图片
     *
     * @param: [id]
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/13 0013 上午 3:11
     */
    @GetMapping("/delimg")
    public CommonResponse deleteImageById(Integer id) {
        CommonResponse response = CommonResponse.success();
        try {
            PageImages image = pageImagesService.findOneById(id);
            if (image == null) {
                response.setCode(404);
                response.setMessage("图片不存在");
            } else {
                pageImagesService.deleteImageById(id);
            }
            return response;
        } catch (Exception e) {
            log.error("delete image error:{}", e);
            response = CommonResponse.serverError();
            return response;
        }
    }
}
